//
//  BanBu_SpecialText.m
//  BanBu
//
//  Created by Wang Xj on 13-9-6.
//
//

#import "BanBu_SpecialText.h"
#import "CJSONSerializer.h"
#import "NSData+Base64.h"
#import "BanBu_ThirdAppWebViewController.h"
#import "BanBu_ChatViewController.h"
#import "AppCommunicationManager.h"
#import "BanBu_PersonDataViewController.h"
#import "LeveyTabBarController.h"
#import "BanBu_PhotoShowViewController.h"
@implementation BanBu_SpecialText

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        UIWebView * webView = [[UIWebView alloc]init];

        webView.layer.cornerRadius = 5.0;
        webView.layer.masksToBounds = YES;
        webView.backgroundColor = [UIColor grayColor];
        self.webView = webView;
        self.webView.frame = CGRectMake(0, 0, 180, 20);
        [self insertSubview:webView atIndex:0];
        [self bringSubviewToFront:self.webView];
        [webView release];
        
        _foregroundView = [[UIView alloc]init];
        _foregroundView.backgroundColor = [UIColor clearColor];
        _foregroundView.userInteractionEnabled = YES;
        _foregroundView.alpha = 1;
        [self addSubview:_foregroundView];
        
        [[UIMenuController sharedMenuController] setMenuVisible:NO animated:YES];
    }
    return self;
}

-(void)setWebViewFrame:(CGRect)frame
{
    CGRect rect = frame;
    rect.origin.x=0;
    rect.origin.y=0;
    self.webView.frame = rect;
    self.webView.layer.cornerRadius = 5.0;
    self.webView.layer.masksToBounds = YES;
    _foregroundView.frame = rect;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    webView.frame = CGRectMake(0, 0, 180, 20);
    
    CGRect frame = webView.frame;
    CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    webView.frame = frame;
    
    if (self.set)
    {
        NSMutableDictionary * amsgDic = [NSMutableDictionary dictionaryWithDictionary:self.amsg];
        if (self.showTime)
        {
            [amsgDic setValue:[NSNumber numberWithDouble:frame.size.height+TimeLabelHeight+2*CellMarge+17] forKey:KeyHeight];
        }
        else
        {
            [amsgDic setValue:[NSNumber numberWithDouble:frame.size.height+2*CellMarge+17] forKey:KeyHeight];
        }
        
        [MyAppDataManager setHeightSp:amsgDic];
        if (!self.cell)
        {
            if (self.alpha == 0)
            {
                [self removeFromSuperview];
            }
        }
    }
    //zyfzyf⬇️
    //国外webView内存泄露解决办法，
    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDiskImageCacheEnabled"];
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitOfflineWebApplicationCacheEnabled"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    //zyfzyfzyf⬆️
}


//设置webView的body
-(void)setContent:(NSString *)content
{
    content = [MyAppDataManager IsInternationalLanguage:content];
    
    if (self.set)
    {
        _webView.delegate = self;
    }
    else
    {
        _webView.delegate = nil;
    }
    NSURL * baseUrl = [NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]];
    [self.webView loadHTMLString:content baseURL:baseUrl];
    [[UIMenuController sharedMenuController] setMenuVisible:NO animated:YES];
}

#pragma mark - 点击网页跳转
-(void)jumpTo:(UITapGestureRecognizer *)tap
{
    if ([[self.linkto valueForKey:@"fc"] isEqualToString:@"toUrl"])//跳转到网页
    {
        BanBu_ThirdAppWebViewController * thirdWebController = [[BanBu_ThirdAppWebViewController alloc]init];
        thirdWebController.view.frame = __MainScreenFrame;
        [thirdWebController setUrlString:[self.linkto valueForKey:@"url"]];
        [_chat.navigationController pushViewController:thirdWebController animated:YES];
        [thirdWebController release];
    }
    else if ([[self.linkto valueForKey:@"fc"]isEqualToString:@"toBroad"])//跳转到广播详情
    {
        //没获取单个人的广播的借口
        NSMutableDictionary *pars = [NSMutableDictionary dictionaryWithCapacity:1];
        [pars setValue:[self.linkto valueForKey:@"actid"] forKey:@"actid"];
        [pars setValue:[MyAppDataManager getLanguageSubmitToServer] forKey:@"lang"];
        //获取到经纬度信息
        [pars setValue:[NSNumber numberWithDouble:(NSInteger)(AppLocationManager.curLocation.longitude*1000000)] forKey:@"plong"];
        [pars setValue:[NSNumber numberWithDouble:(NSInteger)(AppLocationManager.curLocation.latitude*1000000)] forKey:@"plat"];
        
        [TKLoadingView showTkloadingAddedTo:self.chat.navigationController.view title:NSLocalizedString(@"isDownloading", nil) activityAnimated:YES];
        [AppComManager getBanBuData:BanBu_Get_Broadmaster_byid par:pars delegate:self.chat];
    }
    else if ([[self.linkto valueForKey:@"fc"]isEqualToString:@"toProfile"])//跳转到个人资料
    {
        NSLog(@"self.linkto:%@",self.linkto);
        NSDictionary * profile = [NSDictionary dictionaryWithObjectsAndKeys:[self.linkto valueForKey:KeyFromUid],KeyFromUid, nil];
        [self pushnext:profile];
    }
    //2014年11月13日08:36:39 by yinlinlin 添加到相册
    else if ([[self.linkto valueForKey:@"fc"]isEqualToString:@"toAlbum"])//跳转到相册
    {
        BanBu_AlbumModel * album_model = [[BanBu_AlbumModel alloc]init];
        album_model.album_id = [self.linkto valueForKey:@"albumid"];
        album_model.user_id = [self.linkto valueForKey:@"userid"];
        album_model.album_title = [self.linkto valueForKey:@"albumtitle"];
        BanBu_PhotoShowViewController * photoshow = [[BanBu_PhotoShowViewController alloc]init];
        photoshow.albumModel = album_model;
        [album_model release];
        [self.chat.navigationController pushViewController:photoshow animated:YES];
        [photoshow release];
        photoshow = nil;

    }
    else if ([[self.linkto valueForKey:@"fc"]isEqualToString:@"toApp"])//跳转到第三方应用
    {
        NSString * appid = [self.linkto valueForKey:@"appid"];
        NSLog(@"%@",appid);
    }
    else if ([[self.linkto valueForKey:@"fc"]isEqualToString:@"toSelfProfile"])//跳转到自己个人资料，以编辑个人资料
    {
        [self pushSelf];
    }
}

//设置跳转到的界面
-(void)setLinkTo:(NSString *)linkto
{
    self.linkto = [AppComManager getAMsgFrom64String:linkto];
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(jumpTo:)];
    [_foregroundView addGestureRecognizer:tap];
    [tap release];
}

//进他人个人资料
-(void)pushnext:(NSDictionary *)profileDic
{
    NSMutableDictionary * userinfoDic = [NSMutableDictionary dictionaryWithCapacity:5];
    
    [userinfoDic setValue:[profileDic valueForKey:KeyFromUid] forKey:KeyFromUid];
    [userinfoDic setValue:[NSArray array] forKey:KeyFacelist];
    
    BanBu_PersonDataViewController * profileController = nil;

    for(UIViewController *vc in [self.chat.navigationController viewControllers])
        if([vc isKindOfClass:[BanBu_PersonDataViewController class]])
        {
            profileController = (BanBu_PersonDataViewController *)vc;
            break;
        }
    
    if (profileController && [[profileController.profileDic valueForKey:KeyFromUid] isEqualToString:[profileDic valueForKey:KeyFromUid]])
    {
        [self.chat.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        NSDictionary *proDic = [NSDictionary dictionaryWithDictionary:userinfoDic];
        
        profileController = [[BanBu_PersonDataViewController alloc] initWithProfile:proDic];
        
        [self.chat.navigationController pushViewController:profileController animated:YES];
        [profileController release];
    }
}

//编辑自己个人资料
-(void)pushSelf
{
    BanBu_PersonDataViewController *myProfile1 = [[BanBu_PersonDataViewController alloc] initWithProfile:[UserDefaults valueForKey:MyAppDataManager.useruid]];
    [self.chat.navigationController pushViewController:myProfile1 animated:YES];
    [myProfile1 release];
}

-(void)dealloc
{
    [AppComManager cancalHandlesForObject:_chat];
    [_linkto release],_linkto = nil;
    [_webView stopLoading];
    _webView.delegate = nil;
    
    [_webView release],_webView = nil;
    [_foregroundView release],_foregroundView = nil;
    [super dealloc];
}

@end
